package com.xique.door.controller;


import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.api.request.build.RemoteAreaListRequest;
import com.xique.door.bean.domain.BuildArea;
import com.xique.door.bean.request.buildArea.BuildAreaAddRequest;
import com.xique.door.bean.request.buildArea.BuildAreaDeleteRequest;
import com.xique.door.bean.request.buildArea.BuildAreaEditRequest;
import com.xique.door.biz.service.IBuildAreaBizService;
import com.xique.door.service.IBuildAreaService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 区域Controller
 *
 * @author shd
 * @date 2021-09-09
 */
@RestController
@RequestMapping("/community/area")
public class BuildAreaController extends BaseController {

    @Autowired
    private IBuildAreaBizService buildAreaBizService;

    @Autowired
    private IBuildAreaService buildAreaService;

    @Autowired
    private RemoteSystemUserService  remoteSystemUserService;

    /**
     * 查询区域列表
     */
    @PreAuthorize(hasPermi = "door:area:list")
    @GetMapping("/list")
    public TableDataInfo list(BuildArea buildArea) {
        startPage();
        buildArea.setParkId(SecurityUtils.getParkId());
        List<BuildArea> list = buildAreaService.selectBuildAreaList(buildArea);
        List<String> userIds = list.stream().map(e -> e.getUpdateBy()).collect(Collectors.toList());
        if (!userIds.isEmpty()){
            R<List<OperateDTO>> operateList = remoteSystemUserService.getOperateList(StringUtils.join(userIds, ","));
            List<OperateDTO> data = operateList.getData();
            list.forEach(item -> {
                OperateDTO operateDTO = data.stream().filter(e -> String.valueOf(e.getOperateId()).equals(item.getUpdateBy())).findAny().orElse(null);
                if (operateDTO!=null){
                    item.setUpdateBy(operateDTO.getOperateName());
                }
            });
        }


        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize(hasPermi = "door:area:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response,BuildArea buildArea) throws IOException {
        List<BuildArea> list = buildAreaService.selectBuildAreaList(buildArea);
        ExcelUtil<BuildArea> util = new ExcelUtil<BuildArea>(BuildArea.class);
         util.exportExcel(response,list, "BuildArea");
    }


    @GetMapping("/autoGenerateCode")
    public AjaxResult autoGenerateCode() {
        BuildArea buildArea = new BuildArea();
        buildArea.setParkId(SecurityUtils.getParkId());
        List<BuildArea> list = buildAreaService.selectBuildAreaList(buildArea);
        if (list.isEmpty()){
            AjaxResult.success().put("data","01");
        }
        List<Long> collect = list.stream().map(e -> Long.valueOf(e.getCode())).collect(Collectors.toList());
        String code = "";
        if (collect.isEmpty()) {
            code = "01";
        } else {
            Collections.sort(collect, Collections.reverseOrder());
            code = String.valueOf(collect.get(0) + 1);
            if (code.length() == 1) {
                code = 0 + code;
            }
        }

        return AjaxResult.success().put("data",code);
    }

    /**
     * 删除区域
     */
    @PreAuthorize(hasPermi = "door:area:remove")
    @Log(title = "区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("区域删除")
    public AjaxResult removeV2(BuildAreaDeleteRequest request) {
        return buildAreaBizService.areaDelete(request);
    }

    /**
     * 新增保存区域
     */
    @PreAuthorize(hasPermi = "door:area:add")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("区域添加")
    public AjaxResult addSaveV2(@RequestBody BuildAreaAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return buildAreaBizService.areaAdd(request);
    }

    /**
     * 修改保存区域
     */
    @PreAuthorize(hasPermi = "door:area:edit")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation("区域编辑")
    public AjaxResult editSave2(@RequestBody BuildAreaEditRequest request) {
        return buildAreaBizService.areaEdit(request);
    }

    @PostMapping("/openApi/v1/list")
    public AjaxResult areaListV1(@RequestBody RemoteAreaListRequest request) {
        return buildAreaBizService.selectAreaList(request);
    }
}
